package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.infos.ClubDataModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.Editable;
import android.text.InputType;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ModifyTheClubDataActivity extends BaseActivity {

	private EditText Data_xiugai;
	private Button btn_Save;
	private Results results;
	private ClubDataModel dataModel, dataModel1, dataModel2, dataModel3,
			dataModel__;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ModifyTheClubDataActivity.this);
				Utils.showMessage(ModifyTheClubDataActivity.this,
						getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(ModifyTheClubDataActivity.this);
				Utils.showMessage(ModifyTheClubDataActivity.this,
						results.getRetmsg());
				if (results.isRet()) {
					finish();
				}
				break;
			case 3:
				Utils.ExitPrgress(ModifyTheClubDataActivity.this);
				Utils.showMessage(ModifyTheClubDataActivity.this,
						getResources().getString(R.string.l_xa10));
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_modify_the_club_data);
		try {
			dataModel = (ClubDataModel) getIntent().getSerializableExtra(
					"zongzhi");
			dataModel1 = (ClubDataModel) getIntent().getSerializableExtra(
					"zhuqun");
			dataModel2 = (ClubDataModel) getIntent().getSerializableExtra(
					"renshu");
			dataModel3 = (ClubDataModel) getIntent().getSerializableExtra(
					"jinbi");
		} catch (Exception e) {
			// TODO: handle exception
		}

		Data_xiugai = (EditText) this.findViewById(R.id.Data_xiugai);
		btn_Save = (Button) this.findViewById(R.id.btn_Save);
		if (dataModel2 != null || dataModel3 != null) {
			Data_xiugai.setInputType(InputType.TYPE_CLASS_NUMBER);
		}
		btn_Save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String content = Data_xiugai.getText().toString().trim();
				if (content != null) {
					if (dataModel != null) {
						editInfo(dataModel__.getId(), content,
								dataModel__.getSuit_persons(),
								dataModel__.getIs_person(),
								dataModel__.getLimitnum(),
								dataModel__.getIs_gold(),
								dataModel__.getPrice());
					} else if (dataModel1 != null) {
						editInfo(dataModel__.getId(), dataModel__.getInfo(),
								content, dataModel__.getIs_person(),
								dataModel__.getLimitnum(),
								dataModel__.getIs_gold(),
								dataModel__.getPrice());
					} else if (dataModel2 != null) {
						editInfo(dataModel__.getId(), dataModel__.getInfo(),
								dataModel__.getSuit_persons(),
								dataModel__.getIs_person(), content,
								dataModel__.getIs_gold(),
								dataModel__.getPrice());
					} else if (dataModel3 != null) {
						editInfo(dataModel__.getId(), dataModel__.getInfo(),
								dataModel__.getSuit_persons(),
								dataModel__.getIs_person(),
								dataModel__.getLimitnum(),
								dataModel__.getIs_gold(), content);
					}
				} else {
					Utils.showMessage(ModifyTheClubDataActivity.this,
							getResources().getString(R.string.l_xb101));
				}

			}
		});
		this.findViewById(R.id.returnbtn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		if (dataModel != null) {
			dataModel__ = dataModel;
			Data_xiugai.setText(dataModel__.getInfo());
//			   
		} else if (dataModel1 != null) {
			dataModel__ = dataModel1;
			Data_xiugai.setText(dataModel__.getSuit_persons());
//			   
		} else if (dataModel2 != null) {
			dataModel__ = dataModel2;
			Data_xiugai.setText(dataModel__.getLimitnum());
//			   
		} else if (dataModel3 != null) {
			dataModel__ = dataModel3;
			Data_xiugai.setText(dataModel__.getPrice());
//			   
		}
	}

	private void editInfo(final String cid, final String info,
			final String suit_persons, final String is_person,
			final String limitnum, final String is_gold, final String price) {
		Utils.showPrgress(ModifyTheClubDataActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(ModifyTheClubDataActivity.this));
				map.put("cid", cid);// 俱乐部ID
				map.put("info", info);// 俱乐部宗旨
				map.put("suit_persons", suit_persons);// 适合群族
				if (Integer.valueOf(limitnum) != 0) {
					map.put("is_person", "1");// 是否有人数限制( 0不限制 1限制),
				} else {
					map.put("is_person", "0");// 是否有人数限制( 0不限制 1限制),
				}
				if (Integer.valueOf(price) != 0) {
					map.put("is_gold", "1");// 加入是否需要金币(0不需要 1需要),
				} else {
					map.put("is_gold", "0");// 加入是否需要金币(0不需要 1需要),
				}
				map.put("limitnum", limitnum);// 限制人数为多少,当$is_person=0时，$limitnum=0

				map.put("price", price);// 加入所需金币,当$is_gold=0时，$price=0
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/editInfo");
//				   
				if (content != null) {
					results = Utils.checkResult_NNN(
							ModifyTheClubDataActivity.this, content);
//					   
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}
}
